﻿using System.Collections.Generic;
using UnityEngine;
using LitJson;
/**
 * 管理了编号和assetbundle的对应关系。。
 */
namespace Assets.Scripts.Game.ResourceConfig
{

    public  class ResourceLoadConfigManager
    {

        private Dictionary<string, ResConfigInfo> nameInfoPool;
        private Dictionary<int, ResConfigInfo> idInfoPool;


        public ResourceLoadConfigManager()
        {
            nameInfoPool = new Dictionary<string, ResConfigInfo>();
            idInfoPool = new Dictionary<int, ResConfigInfo>();

        }

        private static ResourceLoadConfigManager mgr;

        public static ResourceLoadConfigManager Instance()
        {
            return mgr ?? (mgr = new ResourceLoadConfigManager());
        }


        public bool Init()
        {
            //需要从assetbundle中加载对应的文件
            ioo.resourceManager.CodeLoadAsset("resconfig","Pub_Res_Config.json", ParseConfig);

            return true;
        }


        private void ParseConfig(UnityEngine.Object obj)
        {
            var textObj = obj as TextAsset;

            JsonData jsonObj = JsonMapper.ToObject(textObj.text);
            


            for (int i = 0; i < jsonObj.Count; ++i)
            {
                ResConfigInfo t = new ResConfigInfo();
                t.assetbundle = (string)jsonObj[i]["assetbundle"];
                t.index_id = (int)jsonObj[i]["index_id"];
                t.prefab = (string)jsonObj[i]["prefab"];
                t.res = (string)jsonObj[i]["res"];
                t.res_path = (string)jsonObj[i]["res_path"];
                nameInfoPool[t.res] = t;
                idInfoPool[t.index_id] = t;

            }
        }

        public string GetPrefabNameByName(string name)
        {
            ResConfigInfo value;
            if (nameInfoPool.TryGetValue(name,out value))
            {
                return value.assetbundle;
            }

            return null;
        }

        public string GetPrefabNameByIndex(int index)
        {
            ResConfigInfo value;
            if (idInfoPool.TryGetValue(index, out value))
            {
                return value.assetbundle;
            }

            return null;
        }
    }
}
